package com.hlhj.carview.aty

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MyLocationConfiguration
import com.baidu.mapapi.map.MyLocationData
import com.binioter.guideview.Component
import com.binioter.guideview.DimenUtil.dp2px
import com.binioter.guideview.Guide
import com.binioter.guideview.GuideBuilder
import com.bumptech.glide.Glide
import com.example.mymvp.log
import com.gyf.barlibrary.ImmersionBar
import com.hlhj.carview.BaseAtyContainer
import com.hlhj.carview.Iview.MainView
import com.hlhj.carview.Iview.NavView
import com.hlhj.carview.R
import com.hlhj.carview.Toast
import com.hlhj.carview.bean.WeatherBean
import com.hlhj.carview.customView.MyCompoment
import com.hlhj.carview.presenter.MainPresenter
import com.hlhj.carview.presenter.NavPresenter
import com.hlhj.carview.utils.GlideUtil
import com.hlhj.carview.utils.SPHelper
import kotlinx.android.synthetic.main.aty_nav.*


/**
 * Created by Never Fear   on 2018\12\24 0024.
Never More....
 */
class NavAty :BaseMapAty<NavView,NavPresenter>(), NavView, MainView {
    private lateinit var mainPresenter: MainPresenter
    private var mMapView:BaiduMap?=null
    override fun initMap(savedInstanceState: Bundle?) {
//        mapView.onCreate(savedInstanceState)
    }

    override fun initView() {

        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .init()
        BaseAtyContainer.instance.addActivity(this)



    }

    @SuppressLint("MissingSuperCall")
    override fun onDestroy() {
        super.onDestroy()
        mapView.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        Glide.with(this).load(SPHelper.getHead(this)).apply(GlideUtil.loadHeadOption()).into(btUser)
        mapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        mapView.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        mapView.onSaveInstanceState(outState)
    }
    override fun logicStart() {
        mMapView=mapView.map
        mapView.map.isMyLocationEnabled=true
        mapView.map.setMyLocationConfiguration(MyLocationConfiguration(MyLocationConfiguration.LocationMode.FOLLOWING,false,null))
        mainPresenter?.initLocation(this)

        btMyEquipment.post {
            if (!SPHelper.getFirst(this)){
                return@post
            }
            val builder = GuideBuilder()
            builder.setTargetView(btMyEquipment)
                    .setAlpha(150)
                    .setAutoDismiss(true)
                    .setHighTargetGraphStyle(Component.CIRCLE)
                    .setHighTargetPadding(dp2px(this,20f))
            builder.setOnVisibilityChangedListener(object :
                    GuideBuilder.OnVisibilityChangedListener {
                override fun onShown() {}
                override fun onDismiss() {
//                    showGuideView2()
                }
            })
            var guide: Guide?=null
            val myComponent = MyCompoment()
            builder.addComponent(myComponent)
            guide = builder.createGuide()
            guide.show(this)
            SPHelper.saveFirst(false,this)
        }
    }

    private val REFRESH: Int=111

    override fun setListener() {
        btGiver.setOnClickListener {
            val intent = Intent(this, GiverAty::class.java)
            startActivity(intent)
        }
        btMyEquipment.setOnClickListener {
            val intent = Intent(this, BCarAty::class.java)
            startActivity(intent)
        }
        btUser.setOnClickListener {
            val intent = Intent(this, UserAty::class.java)
            startActivityForResult(intent,REFRESH)
        }
        btMyAdv.setOnClickListener {
            val intent = Intent(this, MyAskListAty::class.java)
            startActivity(intent)
        }
        btGainK.setOnClickListener {
            val intent = Intent(this, ProfitAty::class.java)
            startActivity(intent)
        }

    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        if (requestCode==REFRESH){
            Glide.with(this).load(SPHelper.getHead(this)).into(btUser)
            setResult(-1)
        }
    }

    override fun setLocation(location: BDLocation) {
        val locData = MyLocationData.Builder()
                .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(location.getDirection()).latitude(location.getLatitude())
                .longitude(location.getLongitude()).build()
        mapView.map.setMyLocationData(locData)


    }
    override fun bindView(): NavView {
        return this
    }

    override fun setWeather(weatherBean: WeatherBean) {


    }

    override fun getWeather() {
    }

    override fun createPresenter(): NavPresenter {
        mainPresenter= MainPresenter()
        mainPresenter.createModule()
        mainPresenter.bindView(this)
        return NavPresenter()
    }

    override fun getContentID(): Int {
        return R.layout.aty_nav

    }
}